export default {
    methods: {
        // 执行的接口,参数,成功信息,回调函数
        async $tryCatchFn(option) {
            // eslint-disable-next-line no-async-promise-executor
            return new Promise(async (resolve, reject) => {
                const { fn, data, callBack, loading, success, successFn, errorFn, noLoad } = option;
                if (!noLoad) {
                    this[loading ?? 'loading'] = true;
                }
                try {
                    const res = await fn(data);
                    if (res?.code === 0 || res?.code === 200) {
                        if (success) this.$message.success(success);
                        if (typeof successFn === 'function') {
                            await successFn();
                        }
                    }
                    resolve(res);
                } catch (err) {
                    // if (error)
                    console.log('err错误了', err);
                    this.$message.error(err);
                    if (typeof errorFn === 'function') {
                        await errorFn();
                    }
                    return reject(err);
                } finally {
                    if (typeof callBack === 'function') await callBack();
                    this[loading ?? 'loading'] = false;
                }
            });
        },
        $formAssignment(obj, target) {
            // 判断是否有需要转换为number的字段 转换为number类型
            if (this?.toNumberForm) {
                Object.keys(this.toNumberForm).forEach((key) => {
                    if (obj[key]) {
                        obj[key] = Number(obj[key]);
                    }
                });
            }
            Object.keys(target).forEach((key) => {
                target[key] = obj[key];
            });
            return target;
        },
    },
};
